package com.cloud.consul.comsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;

/**
 * 远程调用
 * @author Thinker
 *
 */
@RestController
@Slf4j
public class CallHelloController {
	@Autowired
	private LoadBalancerClient loadBalancer;

	/**
	 * 远程调用
	 * @return
	 */
	@RequestMapping("/call")
	public String call() {
		ServiceInstance serviceInstance = loadBalancer.choose("spring-cloud-consul-producer");
		log.info("服务地址:" + serviceInstance.getUri());
		log.info("服务名称:" + serviceInstance.getServiceId());

		String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello",
				String.class);
		log.info(callServiceResult);
		return callServiceResult;
	}
}
